/* Copyright 2006 Nicklas R Johnson
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */

package net.spatula.tally_ho.ui.article.read;

import net.spatula.tally_ho.service.ArticleService;
import net.spatula.tally_ho.service.beans.ArticleBean;
import wicket.PageParameters;
import wicket.markup.html.WebPage;
import wicket.markup.html.basic.Label;
import wicket.markup.html.panel.Fragment;
import wicket.util.string.StringValueConversionException;

/**
 * @author spatula
 * 
 */
public class ArticleRead extends WebPage {

    private static final long serialVersionUID = 1L;

    public ArticleRead(PageParameters params) {
        super(params);

        int id = 0;

        try {
            id = params.getInt("id");
        } catch (StringValueConversionException e) {
            Fragment error = new Fragment("fragment", "error");
            error.add(new Label("message", "Problem parsing page parameters; missing or mistyped id?"));
            add(error);
            add(new Label("title", "Error retrieving article"));

            return;
        }

        ArticleBean article = ArticleService.getInstance().getArticleById(id);

        if (article == null) {
            Fragment error = new Fragment("fragment", "error");
            error.add(new Label("message",
                    "Article not found.  The id may have been mistyped or the article has been deleted."));
            add(error);
            
            add(new Label("title", "Error retrieving article"));

            return;
        }

        add(new Label("title", article.getSection().getName() + ":" + " " + article.getHeadline()));
 
        Fragment articleFrag = new Fragment("fragment", "articleDisplay");
        articleFrag.add(new ArticleDisplayPanel("article", article, null));
        add(articleFrag);
        
    }

}
